package cmcciot.onenet.nbapi.sdk.entity;

import cmcciot.onenet.nbapi.sdk.config.Config;
import okhttp3.HttpUrl;
import org.apache.commons.lang3.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author LHL
 * @date: 2018/8/3
 */
public class QueryDatapoints extends CommonEntity {
    private Long deviceId;
    private List<Long> datastreamIdList;
    private LocalDateTime start;
    private LocalDateTime end;
    private Integer limit;
    private Integer cursor;

    public QueryDatapoints(Long deviceId) {
        this.deviceId = deviceId;
    }

    public List<Long> getDatastreamIdList() {
        return datastreamIdList;
    }

    public void setDatastreamIdList(List<Long> datastreamIdList) {
        this.datastreamIdList = datastreamIdList;
    }

    public LocalDateTime getStart() {
        return start;
    }

    public void setStart(LocalDateTime start) {
        this.start = start;
    }

    public LocalDateTime getEnd() {
        return end;
    }

    public void setEnd(LocalDateTime end) {
        this.end = end;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public Integer getCursor() {
        return cursor;
    }

    public void setCursor(Integer cursor) {
        this.cursor = cursor;
    }

    @Override
    public String toUrl() {
        HttpUrl parse = HttpUrl.parse(Config.getDomainName());
        HttpUrl.Builder builder = parse.newBuilder().addPathSegment("devices").addPathSegment(String.valueOf(this.deviceId)).addPathSegment("datapoints");
        if (this.datastreamIdList != null && this.datastreamIdList.size() > 0) {
            builder.addQueryParameter("datastream_id", StringUtils.join(datastreamIdList, ","));
        }
        if (this.start != null) {
            builder.addQueryParameter("start", this.start.withNano(0).toString());
        }
        if (this.end != null) {
            builder.addQueryParameter("end", this.end.withNano(0).toString());
        }
        if (this.limit != null) {
            builder.addQueryParameter("limit", String.valueOf(this.limit));
        }
        if (this.cursor != null) {
            builder.addQueryParameter("cursor", String.valueOf(this.cursor));
        }
        return builder.build().toString();
    }

}
